// pages/operators/withdraw/withdraw.js
var util = require('../../../utils/util.js');
var api = require('../../../config/api.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    vInfo: null,
    info: null,
    amount: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.getVendor();
  },
  getVendor() {
    let self = this;
    wx.getStorage({
      key: 'vInfo',
      success: function(res) {
        console.log(123,res)
        let vInfo = res.data;
        self.setData({
          vInfo
        });
        self.getAmount();
      },
    })

  },
  tixian() {
    if (!this.data.amount) {
      wx.showToast({
        title: '请输入提现金额',
        icon: 'none'
      })
      return;
    }
    if (isNaN(this.data.amount * 1)) {
      wx.showToast({
        title: '格式错误',
        icon: 'none'
      })
      return;
    }
    let vendorId = this.data.vInfo.id;
    let {
      amount
    } = this.data;
    amount = parseFloat(amount).toFixed(2)
    util.request(api.VTransfers, {
      vendorId,
      amount
    }, 'POST').then(r => {
      if (!r.errno) {
        wx.showToast({
          title: '提现成功'
        })
        wx.navigateBack({
          delta: 1
        })
      } else {
        wx.showToast({
          title: r.errmsg,
          icon: 'none'
        })
      }
    }).catch(err => {
      wx.showToast({
        title: err.message,
        icon: 'none'
      })
    })
  },
  changeInput(e) {
    let changed = {};
    let prop = e.currentTarget.dataset.prop
    if (e.detail.value > this.data.info.totalBalance) {
      e.detail.value = this.data.info.totalBalance;
    }
    changed[prop] = e.detail.value
    this.setData(changed)
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  },
  getAmount() {
    let vendorId = this.data.vInfo.id;
    let self = this;
    util.request(api.VAmount, {
      vendorId
    }).then(r => {
      self.setData({
        info: r.data
      })
    })
  }
})
